<template>
  <div id="follow" v-if="show">
    <div class="banktitle">管理已关注指标</div>
    <!-- 接口没有分类先注释掉 -->
    <div class="bankBox" v-for="(item,index) in data" :key="index">
      <div class="boxline">
        <span class="boxcity">{{item.type}}</span>
      </div>
      <div class="yinhang">
        <span v-for="(ele, i) in item.list" :key="i" :class="[ele.attentionState? 'guanzhu':'']" @click="godetail(ele)">{{ele.indicatorName}}</span>
      </div>
    </div>
    <div class="btnCh">
      <el-button type="primary" size="small" @click="cancel">关闭</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'follow',
  data () {
    return {
      show: true,
      checkAll: false,
      // cities: [
      //   {name:'郑州',bank:[
      //     {name: '中国人民银行', guzhu: true},{name: '中国农村信用社',guzhu: false},{name: '邮政储蓄银行',guzhu: true},
      //     {name: '中国人民银行', guzhu: true},{name: '中国农村信用社',guzhu: false},{name: '邮政储蓄银行',guzhu: true},
      //     {name: '中国人民银行', guzhu: true},{name: '中国农村信用社',guzhu: false},{name: '邮政储蓄银行',guzhu: true},
      //     {name: '中国人民银行', guzhu: true},{name: '中国农村信用社',guzhu: false},{name: '邮政储蓄银行',guzhu: true},
      //     ],check: false},
      //   {name:'开心快乐每一天',bank:[{name: '中国人民银行1', guzhu: true},{name: '中国农村信用社1',guzhu: false},{name: '邮政储蓄银行',guzhu: true}],check: false},
      //   {name:'哈哈',bank:[{name: '中国人民银行2', guzhu: true},{name: '中国农村信用社',guzhu: false},{name: '邮政储蓄银行',guzhu: true}],check: false}
      // ],
      // checkedCities:[],
      data: ''
    };
  },
  mounted () {
    this.$bus.$on('chooseBank', config => {
      if (!config) return (this.show = false);
      this.callback = config.callback;
      this.show = true;
    });
    this.getFollow();
  },
  methods: {
    getFollow() {
       this.axios.get('userAttention/getAllAttention')
        .then(({data}) => {
          if (data.code == 200) {
            this.data = data.data;
          }
        })
        .catch(function (error) {
          console.log(error);
        });  
     },
    godetail (item) {
      this.$router.push({path: 'targetDetail', query:{id: item.siId, title: item.indicatorName}});
    },
    noallchoose () {
      this.cities.forEach((item,index) =>{
        item.check = false;
        this.checkedCities[index].bank= [];
      })
    },
    allchoose () {
      this.cities.forEach((item,index) =>{
        item.check = true;
        this.checkedCities[index].bank= item.bank;
      })
    },
    wancheng () {
      let status = false;
      this.checkedCities.forEach((item,index)=> {
        if (item.bank.length != 0) {
          status = true;
        }
      })
      if (!status) {
        this.$message({message: '请您选择社行/机构',type: 'warning'});
        return false;
      }
      let banks=[];
      this.checkedCities.forEach((item, index)=> {
        item.bank.forEach((ele, i)=> {
          banks.push(ele);
        })
      })
      this.callback(this.checkedCities, banks);
      this.show= false;
    },
    cancel () {
      this.$router.go(-1);
    },
    // 单个城市的全选
    cityall (index, item) {
      if (item.check) {
        this.checkedCities[index].bank= item.bank;
      } else {        
        this.checkedCities[index].bank= [];
      }
    },
    newchecked () {
      this.cities.forEach((item,index)=> {
        this.checkedCities.push({name: item.name, bank:[]})
      })
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.cities : [];
    },
    handleCheckedCitiesChange(value, index) {
      let checkedCount = value.length;
      this.cities[index].check = checkedCount === this.cities[index].bank.length;
    }

  },
  watch: {
    // 每次都清空选中的值
    show () {
      if (this.cities.length != 0) {
        this.cities.forEach((item,index) =>{
          item.check = false;
          this.checkedCities[index].bank= [];
        })
      } 
    }
  }
};
</script>

<style lang="scss">
  #follow {
    overflow-y: auto;
    background-color: rgb(225, 226, 220);
    padding-bottom: 20px;
    // background-color: white;
    .banktitle {
      height: 50px;
      line-height: 50px;
      font-size: 16px;
      padding-left: 20px;
      background-color:#809092;
      border-bottom: 1px solid #ccc;
      color: white;
    }
    .bankBox {
      .boxline{
        padding: 0 20px;
        display: flex;
        justify-content: space-between;
        height: 40px;
        align-items: center;
        background-color: #F0F0F0;
      }
    }
    .yinhang {
      background-color: white;
      padding: 20px;
      // padding-top: 10px;
      span {
        display: inline-block;
        margin-top: 10px;
        padding: 5px 10px;
        border: 1px solid black;
        margin-right: 20px;
        cursor: pointer;
        &.guanzhu {
          background-color: #409EFF;
        }
      }  
    }
    .btnCh {
      margin-top: 20px;
      display: flex;
      justify-content: center;
      .el-button {
        margin-right: 20px;
      }
    }
  }
</style>
